import { createStore } from 'vuex'
import { getInfo, getWether, getUserInfo, updateUserInfo, releaseNote, getAllNoteByUserId, getAllNote } from "../api/interface";

export default createStore({
  state: {
    // 下面四个是Json数据
    birdData: [],
    placeData: [],
    coupe: [],
    coupeData: [],
    // 主页轮播图
    swipeImgList: [],
    // 天气模块
    wether: [],
    // 用户表
    userinfo: {},
    // 我的笔记列表
    myNote: [],
    // 系统展示数据
    system: {
    },
    // 主页推荐的笔记列表
    allNote: []
  },
  mutations: {
    updateBirdData(state, param) {
      state.birdData = param
    },
    updateCoupe(state, param) {
      state.coupe = param
    },
    updateCoupeData(state, param) {
      state.coupeData = param
    },
    updateMyNote(state, param) {
      state.myNote = param
    },
    updatePlaceData(state, param) {
      state.placeData = param
    },
    updateSwipeImgList(state, param) {
      state.swipeImgList = param
    },
    updateWether(state, param) {
      state.wether = param
    },
    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo
    },
    updataAllNote(state, param) {
      state.allNote = param
    }
  },
  actions: {
    async _getJsonInfo(context) {
      await getInfo()
        .then((res) => {
          context.commit('updateBirdData', res.birdData)
          context.commit('updatePlaceData', res.placeData)
          context.commit('updateSwipeImgList', res.swipeImgList)
          context.commit('updateCoupe', res.coupe)
          context.commit('updateCoupeData', res.coupeData)
        })
    },
    async _getUserInfo(context) {
      await getUserInfo().then(res => {
        context.commit('updateUserInfo', res.data)
      })
    },

    async _updateUserInfo(context, newData) {
      await updateUserInfo(newData)
        .then((res) => {
          context.commit('updateUserInfo', res.data)
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    async _getWether(context) {
      await getWether()
        .then((res) => {
          context.commit('updateWether', res.data[0].daily)
        })
    },

    async _releaseNote(context, data) {
      await releaseNote({
        author: data.author,
        content: data.content,
        imgList: data.imgList,
        createTime: data.createTime,
        place: data.place,
        tag: data.tag,
        title: data.title,
      }).then(() => {
      })
    },
    async _getAllNoteByUserId(context, id) {
      await getAllNoteByUserId({ id: id }).then(res => {
        context.commit('updateMyNote', res.data.reverse())
      })
    },
    async _getAllNote(context) {
      await getAllNote().then(res => {
        context.commit('updataAllNote', res.data.reverse())
      })
    }
  },
  modules: {
  }
})
